package com.common.convert;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.converter.GenericConverter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Set;

/**
 * @author 大忽悠
 * @create 2022/11/11 10:46
 */
@Slf4j
public class LocalDateTimeConvert implements GenericConverter{
    private final DateTimeFormatter dateTimeFormatter=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public Set<ConvertiblePair> getConvertibleTypes() {
        return Set.of(new ConvertiblePair(String.class, LocalDateTime.class));
    }


    @Override
    public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
        log.info("待转换的目标日期字符串为: {}",source);
        return LocalDateTime.parse((CharSequence) source, dateTimeFormatter);
    }
}
